package com.stonedt.spider.controller.datasource;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stonedt.spider.entity.DataSource;
import com.stonedt.spider.entity.SourceSite;
import com.stonedt.spider.entity.SpiderTestData;
import com.stonedt.spider.service.SourceSiteService;
import com.stonedt.spider.service.WebsiteshieService;
import com.stonedt.spider.util.ResultUtil;
import com.stonedt.spider.util.URLUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/datasourcesite")
public class SourceSiteController {


    @Autowired
    SourceSiteService sourceSiteService;

    @Autowired
    private WebsiteshieService websiteshieService;

    /***
     * 详情页面
     * @param
     * @return
     */
    @RequestMapping(value = "/detail/{website_id}")
    public ModelAndView detail(@PathVariable Integer website_id, ModelAndView mv, HttpServletRequest request) {

        SourceSite sourceSite = sourceSiteService.searchSiteById(website_id);
        mv.setViewName("datasource/website_info");
        mv.addObject("website", sourceSite);
        return mv;
    }
    
    /***
     * 详情页面-招投标
     * @param
     * @return
     */
    @RequestMapping(value = "/BidDetail/{website_id}")
    public ModelAndView BidDetail(@PathVariable Integer website_id, ModelAndView mv, HttpServletRequest request) {

        SourceSite sourceSite = sourceSiteService.searchBidSiteById(website_id);
        mv.setViewName("datasource/BidWebsite_info");
        mv.addObject("website", sourceSite);
        return mv;
    }


    /***
     * 修改状态
     * @param
     * @return
     */
    @RequestMapping(value = "/state/{state}/{id}")
    @ResponseBody
    public ResultUtil state(@PathVariable Integer state,@PathVariable Integer id) throws Exception {
        ResultUtil resultUtil=new ResultUtil();
          sourceSiteService.state(state,id);
          resultUtil.setStatus(200);
          return resultUtil;
    }
    
    /***
     * 修改状态-招投标
     * @param
     * @return
     */
    @RequestMapping(value = "/Bidstate/{state}/{id}")
    @ResponseBody
    public ResultUtil Bidstate(@PathVariable Integer state,@PathVariable Integer id) throws Exception {
        ResultUtil resultUtil=new ResultUtil();
          sourceSiteService.Bidstate(state,id);
          resultUtil.setStatus(200);
          return resultUtil;
    }
    
    /***
     * 清除临时spider_test_data表数据改变状态state为1
     * @param
     * @return
     */
    @RequestMapping(value = "/eliminate/{id}")
    @ResponseBody
    public ResultUtil eliminate(@PathVariable Integer id) throws Exception {
        ResultUtil resultUtil=new ResultUtil();
          sourceSiteService.eliminate(id);
          resultUtil.setStatus(200);
          return resultUtil;
    }
    
    /***
     * 清除临时bid_spider_test_data表数据改变状态state为1-招投标
     * @param
     * @return
     */
    @RequestMapping(value = "/bidEliminate/{id}")
    @ResponseBody
    public ResultUtil bidEliminate(@PathVariable Integer id) throws Exception {
        ResultUtil resultUtil=new ResultUtil();
          sourceSiteService.bidEliminate(id);
          resultUtil.setStatus(200);
          return resultUtil;
    }
    
    /***
     * 查看当前站点抓取的栏目列表
     * @param
     * @return
     */
    @RequestMapping(value = "/see_section/{id}/{page_num}/{page_size}")
    @ResponseBody
    public ResultUtil seeSection(@PathVariable Integer id,@PathVariable Integer page_num,@PathVariable Integer page_size) throws Exception {
        ResultUtil resultUtil = new ResultUtil();
        PageHelper.startPage(page_num, page_size);
        ArrayList<SpiderTestData> list = sourceSiteService.seeSection(id);
        PageInfo<SpiderTestData> pageInfo = new PageInfo<SpiderTestData>(list);
//        int pages = count % pageSize == 0 ? count/pageSize : count/pageSize +1;
//        pageInfo.setTotal(list.size());
//        pageInfo.setPageSize(page_size);
//        pageInfo.setPages(page_num);
        resultUtil.setStatus(200);
        resultUtil.setData(pageInfo);
        return resultUtil;
    }
    
    /***
     * 查看当前站点抓取的栏目列表-招投标
     * @param
     * @return
     */
    @RequestMapping(value = "/seeBid_section/{id}/{page_num}/{page_size}")
    @ResponseBody
    public ResultUtil seeBidSection(@PathVariable Integer id,@PathVariable Integer page_num,@PathVariable Integer page_size) throws Exception {
        ResultUtil resultUtil = new ResultUtil();
        PageHelper.startPage(page_num, page_size);
        ArrayList<SpiderTestData> list = sourceSiteService.seeBidSection(id);
        PageInfo<SpiderTestData> pageInfo = new PageInfo<SpiderTestData>(list);
        resultUtil.setStatus(200);
        resultUtil.setData(pageInfo);
        return resultUtil;
    }



    /**
     * @return
     * @throws
     * @author dxk
     * @date 2021/7/26 14:58
     * 跳转新增站点页面
     * @since
     */
    @RequestMapping("/addwebsite")
    public ModelAndView addwebsite(ModelAndView mv) {
        mv.setViewName("website/add_website");
        return mv;
    }
    
    /**
     * @return
     * @throws
     * @author dxk
     * @date 2021/7/26 14:58
     * 跳转新增站点页面-招投标
     * @since
     */
    @RequestMapping("/addBidWebsite")
    public ModelAndView addBidWebsite(ModelAndView mv) {
        mv.setViewName("website/add_BidWebsite");
        return mv;
    }

    /**
     * @return
     * @throws
     * @author dxk
     * @date 2021/7/26 16:58
     * 保存站点
     * @since
     */
    @RequestMapping("/saveWeb")
    @ResponseBody
    public ResultUtil spiderSave(DataSource dataSource) {
        try {
            String websiteUrl = dataSource.getWebsiteUrl();
            String one_domain = URLUtil.getDomain(websiteUrl, 1);
            String two_domain = URLUtil.getDomain(websiteUrl, 2);
            String domainName = URLUtil.getDomainName(websiteUrl);
            dataSource.setOneDomain(one_domain);
            dataSource.setTwoDomain(two_domain);
            dataSource.setSitedomain(domainName);
            DataSource dataSource2 = websiteshieService.seleteWebsiteByTwoDomain(two_domain);
            if (dataSource2 != null) {
                //更新内容
                dataSource.setId(dataSource2.getId());
                websiteshieService.updateWebSite(dataSource);
            } else {
                //新增内容
                websiteshieService.insertWebSite(dataSource);
            }
            return new ResultUtil(dataSource);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * @return
     * @throws
     * @author dxk
     * @date 2021/7/26 16:58
     * 保存站点-招投标
     * @since
     */
    @RequestMapping("/saveWebBid")
    @ResponseBody
    public ResultUtil saveWebBid(DataSource dataSource) {
        try {
            String websiteUrl = dataSource.getWebsiteUrl();
            String one_domain = URLUtil.getDomain(websiteUrl, 1);
            String two_domain = URLUtil.getDomain(websiteUrl, 2);
            String domainName = URLUtil.getDomainName(websiteUrl);
            dataSource.setOneDomain(one_domain);
            dataSource.setTwoDomain(two_domain);
            dataSource.setSitedomain(domainName);
            DataSource dataSource2 = websiteshieService.seleteBidWebsiteByTwoDomain(two_domain);
            if (dataSource2 != null) {
                //更新内容
                dataSource.setId(dataSource2.getId());
                websiteshieService.updateBidWebSite(dataSource);
            } else {
                //新增内容
                websiteshieService.insertBidWebSite(dataSource);
            }
            return new ResultUtil(dataSource);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author dxk
     * @date 2021/7/26 17:46
       通过url获取一二级域名
     * @return
     * @throws
     * @since
    */
    @RequestMapping("/getDomain")
    @ResponseBody
    public Map spiderSave(String url) {
		Map map = new HashMap();
        String one_domain = URLUtil.getDomain(url, 1);
        String two_domain = URLUtil.getDomain(url, 2);
        map.put("one_domain",one_domain);
		map.put("two_domain",two_domain);
        return map;
    }

}
